package com.dindang.maintenance.mvp.ui.adapter.custom;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dindang.maintenance.R;
import com.dindang.maintenance.mvp.model.entity.Repair;

import java.util.List;

/**维修执行人员 数据适配器*/
public class MaintainExecutiveViewAdapter extends RecyclerView.Adapter<MaintainExecutiveViewAdapter.ViewHolder> {

    List<Repair.MaintenancePeople> list;//执行人员集合
    int width = 0;//item宽度
    private OnClickListener listener;
    public MaintainExecutiveViewAdapter(List<Repair.MaintenancePeople> list) {
        this.list = list;
    }

    public void setListener(OnClickListener listener){
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.maintenance_item_executive, parent, false);
        MaintainExecutiveViewAdapter.ViewHolder viewHolder = new MaintainExecutiveViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Repair.MaintenancePeople executive = list.get(position);
        ViewGroup.LayoutParams layoutParams = holder.ll_executive.getLayoutParams();
        layoutParams.width = width;

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(holder.ll_executive.getLayoutParams());
        if(position == 0){
            lp.setMargins(20, 0, 0, 0);
        }else{
            lp.setMargins(0, 0, 0, 0);
        }
        holder.ll_executive.setLayoutParams(lp);

        ViewGroup.LayoutParams flLayoutParams = holder.fl_head.getLayoutParams();
        flLayoutParams.width = width - 60;
        flLayoutParams.height = flLayoutParams.width;

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.maintenance_ic_load_image)
                .error(R.mipmap.maintenance_ic_load_image);

        Glide.with(holder.iv_head.getContext())
                .asBitmap()
                .apply(options)
                .load("http://img2.tgbusdata.cn/v2/thumb/jpg/NjE1LDYwMCwyNDAsNCwzLDEsLTEsMCx0cjY1/u/ps4.tgbus.com/UploadFiles/201803/20180320112423849.jpg")
                .into(holder.iv_head);

        holder.tv_name.setText(executive.c_username);
        holder.tv_tasksnum.setText("已有" + (executive.doing <= 99 ? executive.doing : "99+") + "条任务");

        if(executive.isSelect()){
            holder.iv_select.setVisibility(View.VISIBLE);
        }else{
            holder.iv_select.setVisibility(View.GONE);
        }

        holder.ll_executive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onClick(position);
                }
            }
        });

        holder.iv_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setExecutiveWidth(int width) {
        this.width = width / 4;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_executive;
        FrameLayout fl_head;
        ImageView iv_head;
        ImageView iv_select;
        TextView tv_name;
        TextView tv_tasksnum;
        ViewHolder(View itemView) {
            super(itemView);
            ll_executive = itemView.findViewById(R.id.maintenance_ll_executive);
            fl_head = itemView.findViewById(R.id.maintenance_fl_head);
            iv_head = itemView.findViewById(R.id.maintenance_iv_head);
            iv_select = itemView.findViewById(R.id.maintenance_iv_select);
            tv_name = itemView.findViewById(R.id.maintenance_tv_name);
            tv_tasksnum = itemView.findViewById(R.id.maintenance_tv_tasksnum);
        }
    }

    public interface OnClickListener{
        void onClick(int position);
    }

}